/**
 * Copyright (c) 2002-2012 旭鸣信息
 * All rights reserved. 
 * File：FrontUserCommentDaoImpl.java
 * History: Dec 27, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.IFrontUserCommentDao;
import com.xteam.ecsystem.entities.BaseReplyInfo;
import com.xteam.ecsystem.entities.BaseUserComment;
import com.xteam.ecsystem.utils.PageCenterStation;

/**
 * 前端用户评论数据访问实现类
 * @author liuzhi
 *
 */
@Repository
public class FrontUserCommentDaoImpl extends DDLPrefix implements IFrontUserCommentDao {
	
	@SuppressWarnings("unchecked")
	@Autowired
	private BaseDaoImpl baseDao;

	@SuppressWarnings("unchecked")
	public PageCenterStation<?> getUserComments(String userEmail, int pageIndex, int pageSize) {
		try {
			List list = new ArrayList();
			
			String sql = "SELECT buc.buc_id AS bucId, buc.buc_name AS bucName, buc.buc_email AS bucEmail, "
					+ "buc.buc_type AS bucType, buc.buc_target AS bucTarget, buc.buc_title AS bucTitle, " 
					+ "buc.buc_rank AS bucRank, buc.buc_content AS bucContent, buc.buc_ip AS bucIp, " 
					+ "buc.buc_time AS bucTime, buc.buc_state AS bucState, bgi.bgi_name AS goodsName, "
					+ "bai.bai_title AS articleName, bri.bri_content AS replyContent "
					+ "FROM base_user_comment AS buc "
					+ "LEFT JOIN base_goods_info AS bgi ON buc.buc_target = bgi.bgi_id "
					+ "LEFT JOIN base_article_info AS bai ON buc.buc_target = bai.bai_id "
					+ "LEFT JOIN base_reply_info AS bri ON buc.buc_id = bri.bri_target "
					+ "WHERE buc.buc_state = 1 AND buc.buc_email = ? "
					+ "ORDER BY buc.buc_time ASC";
			
			String countSql = "SELECT COUNT(*) FROM base_user_comment WHERE buc_state = 1 AND buc_email = ?";
			
			list.add(userEmail);
			
			PageCenterStation<?> p = (PageCenterStation<?>) this.baseDao.
					queryWithSQL(BaseUserComment.class, countSql, sql, null,
							list.toArray(), (pageIndex - 1) * pageSize, pageSize);
			
			
			return p;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	public void delUserCmt(String bucId) {
		try {
			String hql = SQL_DELETE + SQL_FROM + BaseUserComment.REF + SQL_WHERE + "bucId = ?";
			
			this.baseDao.executeUpdate(hql, new Object[]{bucId});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delUserReplyCmt(String bucId) {
		try {
			String hql = SQL_DELETE + SQL_FROM + BaseReplyInfo.REF + SQL_WHERE + "briTarget = ?";
			
			this.baseDao.executeUpdate(hql, new Object[]{bucId});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
